
#include <bits/stdc++.h>

using namespace std;

const int N = 5e5+7;
int l[N],r[N],sum[N],a[N];

int main() {
    int n;
    while(~scanf("%d",&n)){

        memset(sum,0,sizeof(sum));
        a[0] = a[n+1] = -1;

        for(int i = 1;i<=n;i++){
            scanf("%d",&a[i]);
            sum[i] = sum[i-1] + a[i]; // 递增和
        }

        stack<int>s;
        s.push(0);
        for(int i = 1; i<=n; i++) {
            int x;
            for(x = s.top();a[x]>=a[i];x = s.top()){
                s.pop();
            }
            l[i] = x+1;
            s.push(i);
        }

        while(!s.empty())s.pop();
        s.push(n+1);
        for(int i = n;i>=1;i--){
            int x;
            for(x = s.top();a[x]>=a[i];x = s.top()){
                s.pop();
            }
            r[i] = x-1;
            s.push(i);
        }

        int mx = 0;
        for(int i = 1;i<=n;i++){
            mx = max(mx,a[i]*(sum[r[i]]-sum[l[i]-1]));
        }
        printf("%d\n",mx);
    }
    return 0;
}
